package ph.com.gs3.formalistics.model.facades;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import ph.com.gs3.formalistics.model.ldao.FormsLDAO;
import ph.com.gs3.formalistics.model.vo.business.User;
import ph.com.gs3.formalistics.model.vo.business.form.Form;
import ph.com.gs3.formalistics.model.vo.business.form.FormFieldData.InvalidFormFieldException;
import android.content.Context;

public class FormsDataReader {

	private FormsLDAO formsLDAO;
	private User user;

	public FormsDataReader(Context context, User user) {
		formsLDAO = new FormsLDAO(context);
		this.user = user;
	}

	public List<Form> getCompanyForms() {
		List<Form> forms = new ArrayList<>();

		int companyId = user.getCompany().getId();

		// Get all forms to sync documents
		try {
			formsLDAO.open();
			forms = formsLDAO.getCompanyForms(companyId);
		} catch (JSONException | InvalidFormFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			formsLDAO.close();
		}

		return forms;
	}

	public Form getForm(int id) {

		Form form = null;

		try {
			formsLDAO.open();
			form = formsLDAO.getForm(id);
		} catch (JSONException | InvalidFormFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			formsLDAO.close();
		}

		return form;

	}

	public Form getForm(String webId, int companyId) {

		Form form = null;

		try {
			formsLDAO.open();
			form = formsLDAO.getForm(webId, companyId);
		} catch (JSONException | InvalidFormFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			formsLDAO.close();
		}

		return form;

	}

}
